package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.currentdesc;

import java.util.Collection;

import com.google.common.base.Optional;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorContextMapper;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorCtx;
import edu.stanford.bmir.protege.web.client.ui.editor.EditorPresenter;
import edu.stanford.bmir.protege.web.client.ui.editor.EntityDataContextSelector;
import edu.stanford.bmir.protege.web.client.ui.editor.OWLEntityDataContext;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.portlet.AbstractOWLEntityPortlet;
import edu.stanford.bmir.protege.web.shared.entity.OWLEntityData;

public class StkosCutterCurrentDescriptionPortlet extends
		AbstractOWLEntityPortlet {

	private TreeNode selectedTreeNode;

	private EditorPresenter editorPresenter;

	public StkosCutterCurrentDescriptionPortlet(Project project) {
		super(project);
	}

	@Override
	public Collection<EntityData> getSelection() {
		return null;
	}

	@Override
	public void onRefresh() {
		if (editorPresenter != null) {
			editorPresenter.setEditorContext(getEditorContext());
		}
	}

	@Override
	public void reload() {
	}

	@Override
	public void initialize() {
		setHeight(210);

		setSelectedTreeNode(StkosPanelType.CLASS_TREE, null);
	}

	public void setSelectedTreeNode(StkosPanelType type,
			TreeNode selectedTreeNode) {
		this.selectedTreeNode = selectedTreeNode;
		if (StkosPanelType.CLASS_TREE.equals(type)) {
			setTitle("当前本体类描述");
		} else if (StkosPanelType.PROPERTY_TREE.equals(type)) {
			setTitle("当前本体属性描述");
		} else if (StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			setTitle("当前本体个体描述");
		} else {
			setTitle("当前本体描述");
		}
		if (editorPresenter == null) {
			addEditorPresenter();
			if (editorPresenter == null) {
				return;
			}
		}
		editorPresenter.setEditorContext(getEditorContext());
	}

	protected void addEditorPresenter() {
		editorPresenter = null;
		EditorContextMapper contextMapper = new EditorContextMapper();
		contextMapper.registerSelector(new EntityDataContextSelector());
		editorPresenter = new EditorPresenter(getProjectId(), contextMapper);
		if (editorPresenter == null) {
			return;
		}
		ScrollPanel sp = new ScrollPanel(editorPresenter.getEditorHolder());
		sp.setSize("100%", "100%");
		add(sp);
		doLayout();
	}

	protected Optional<EditorCtx> getEditorContext() {
		if (selectedTreeNode == null) {
			return Optional.absent();
		}
		if (selectedTreeNode.getUserObject() == null) {
			return Optional.absent();
		}
		if (getProject() == null || getProject().getProjectId() == null) {
			return Optional.absent();
		}
		Optional<OWLEntityData> sel = Optional.absent();
		if (selectedTreeNode.getUserObject() instanceof OWLEntityData) {
			sel = Optional.<OWLEntityData> of((OWLEntityData) selectedTreeNode
					.getUserObject());
		} else if (selectedTreeNode.getUserObject() instanceof EntityData) {
			sel = toOWLEntityData((EntityData) selectedTreeNode.getUserObject());
		} else {
			GWT.log(selectedTreeNode.getUserObject().toString());
		}
		if (!sel.isPresent()) {
			return Optional.absent();
		}
		return Optional.<EditorCtx> of(new OWLEntityDataContext(getProject()
				.getProjectId(), sel.get()));
	}
}
